<?php

namespace app\admin\controller;
use app\common\controller\Core;

class Files extends Core
{

	// 编辑器上传配置
	public function upload() {
		$_Uid = input ( 'uid','sys' ); // 上传标识
		$_FileName = input ( 'file_name' ,'files'); // 上传文件名
		$_Type = input ( 'dir' ,'file'); // 上传文件类型
		if (empty ( $_Uid )) {
			$_Return ['error'] = 1;
			$_Return ['message'] = '没有登录不允许上传';
			exit ( json_encode ( $_Return ) );
		}
		/* 返回标准数据 */
		$_Return = array (
			'error' => 0 
		);
		$_File = request()->file($_FileName);
		if(!$_File){
			$_Return ['error'] = 1;
			$_Return ['message'] = '未获取到上传文件';
			exit ( json_encode ( $_Return ) );
		}
		if(!in_array($_Type,config('upload_type_list'))){
			$_Return ['error'] = 1;
			$_Return ['message'] = '上传类型未注册';
			exit ( json_encode ( $_Return ) );
		}
		// 判断文件上传类型
		$_TypeSize	= config('upload_'.$_Type.'_size');
		$_TypeExt	= config('upload_'.$_Type.'_ext');
		// 移动到框架应用根目录/public/uploads/ 目录下
		$_Info = $_File->validate(['size'=>$_TypeSize,'ext'=>$_TypeExt])->move(ROOT_PATH . 'static/upload/user' . DIRECTORY_SEPARATOR.$_Uid.DIRECTORY_SEPARATOR.$_Type.DIRECTORY_SEPARATOR);
		if($_Info){
			$_Return ['url'] = WEB_PATH .'/static/upload/user' . DIRECTORY_SEPARATOR.$_Uid.DIRECTORY_SEPARATOR.$_Type.DIRECTORY_SEPARATOR. $_Info->getSaveName();
		}else{
			$_Return ['error'] = 1;
			$_Return ['message'] = $_File->getError();
			// 上传失败获取错误信息
			echo $_File->getError();
		}
		exit ( json_encode ( $_Return ) );
	}
	// 编辑器浏览器设置
	public function filemanager() {
		$_Uid = input ( 'uid','sys' ); // 上传标识
		$_Type = input ( 'dir' ,'file'); // 上传文件类型
		$_Path = input ( 'path' ,''); // 上传文件类型
		if (empty ( $_Uid )) {
			$_Return ['error'] = 1;
			$_Return ['message'] = '没有登录无法获取';
			exit ( json_encode ( $_Return ) );
		}
		// 根目录路径，可以指定绝对路径，比如 /var/www/attached/
		$_RootPath = ROOT_PATH . 'static/upload/user' . DIRECTORY_SEPARATOR.$_Uid;
		// 根目录URL，可以指定绝对路径，比如 http://www.yoursite.com/attached/
		$_RootUrl = WEB_PATH .'/static/upload/user' . DIRECTORY_SEPARATOR.$_Uid;
		
		if(!in_array($_Type,config('upload_type_list'))){
			$_Return ['error'] = 1;
			$_Return ['message'] = '上传类型未注册';
			exit ( json_encode ( $_Return ) );
		}
		
		$_ImgTypeExt	= explode(',',config('upload_'.$_Type.'_ext'));
		
		if ($_Type !== '') {
			$_RootPath	= $_RootPath . DIRECTORY_SEPARATOR;
			$_RootUrl	= $_RootUrl . DIRECTORY_SEPARATOR;
			if (! file_exists ( $_RootPath )) {
				$_Res = by_dir ( $_RootPath );
				if(!$_Res){
					$_Return ['error'] = 1;
					$_Return ['message'] = '创建文件夹失败';
					exit ( json_encode ( $_Return ) );
				}
			}
		}else{
			$_Return ['error'] = 1;
			$_Return ['message'] = '路径不存在';
			exit ( json_encode ( $_Return ) );
		}
		
		// 根据path参数，设置各路径和URL
		if ($_Path == '') {
			$_CurrentPath	= $_RootPath . $_Type. '/';
			$_CurrentUrl	= $_RootUrl . $_Type;
			$_CurrentDirPath	= '';
			$_MoveupDirPath		= '';
		} else {
			$_CurrentPath	= $_RootPath . $_Type. '/' . $_Path;
			$_CurrentUrl	= $_RootUrl . $_Type. '/' . $_Path;
			$_CurrentDirPath	= $_Path;
			$_MoveupDirPath = preg_replace ( '/(.*?)[^\/]+\/$/', '$1', $_CurrentDirPath );
		}
		// 不允许使用..移动到上一级目录
		if (preg_match ( '/\.\./', $_CurrentPath )) {
			$_Return ['error'] = 1;
			$_Return ['message'] = '不允许使用..移动到上一级目录';
			exit ( json_encode ( $_Return ) );
		}
		// 最后一个字符不是/
		if (! preg_match ( '/\/$/', $_CurrentPath )) {
			$_Return ['error'] = 1;
			$_Return ['message'] = '目录错误';
			exit ( json_encode ( $_Return ) );
		}
		// 目录不存在或不是目录
		if (! file_exists ( $_CurrentPath ) || ! is_dir ( $_CurrentPath )) {
			$_Return ['error'] = 1;
			$_Return ['message'] = '目录不存在';
			exit ( json_encode ( $_Return ) );
		}
		// 遍历目录取得文件信息
		$_FileList = array ();
		if ($_Handle = opendir ( $_CurrentPath )) {
			$i = 0;
			while ( false !== ($_FileName = readdir ( $_Handle )) ) {
				if ($_FileName {0} == '.'){
					continue;
				}
				$_File = $_CurrentPath . $_FileName;
				if (is_dir ( $_File )) {
					$_FileList [$i] ['is_dir'] = true; // 是否文件夹
					$_FileList [$i] ['has_file'] = (count ( scandir ( $_File ) ) > 2); // 文件夹是否包含文件
					$_FileList [$i] ['filesize'] = 0; // 文件大小
					$_FileList [$i] ['is_photo'] = false; // 是否图片
					$_FileList [$i] ['filetype'] = ''; // 文件类别，用扩展名判断
				} else {
					$_FileList [$i] ['is_dir'] = false;
					$_FileList [$i] ['has_file'] = false;
					$_FileList [$i] ['filesize'] = filesize ( $_File );
					$_FileList [$i] ['dir_path'] = '';
					$_FileExt = strtolower ( pathinfo ( $_File, PATHINFO_EXTENSION ) );
					$_FileList [$i] ['is_photo'] = in_array ( $_FileExt, $_ImgTypeExt );
					$_FileList [$i] ['filetype'] = $_FileExt;
				}
				$_FileList [$i] ['filename'] = $_FileName; // 文件名，包含扩展名
				$_FileList [$i] ['datetime'] = date ( 'Y-m-d H:i:s', filemtime ( $_File ) ); // 文件最后修改时间
				$i ++;
			}
			closedir ( $_Handle );
		}
		
		// 排序形式，name or size or type
		$_Order = strtolower(input('order','name'));
		$_Sorts = array ();
		foreach ( $_FileList as $_File ) {
			$_Sorts ['size'] [] = $_File ['filesize'];
			$_Sorts ['type'] [] = $_File ['filetype'];
			$_Sorts ['name'] [] = $_File ['filename'];
			$_Sorts ['datetime'][] = $_File['datetime']; //时间排序
		}
		if ($_Order == 'name') {
			array_multisort ( $_Sorts ['name'], SORT_ASC, $_FileList );
		}
		if ($_Order == 'size') {
			array_multisort ( $_Sorts ['size'], SORT_DESC, $_FileList );
		}
		if ($_Order == 'type') {
			array_multisort ( $_Sorts ['type'], SORT_ASC, $_FileList );
		}
		if($_Order=='datetime'){
			array_multisort($_Sorts['datetime'], SORT_ASC, $_FileList);
		}
		
		$_Return = [];
		// 相对于根目录的上一级目录
		$_Return ['moveup_dir_path'] = $_MoveupDirPath;
		// 相对于根目录的当前目录
		$_Return ['current_dir_path'] = $_CurrentDirPath;
		// 当前目录的URL
		$_Return ['current_url'] = $_CurrentUrl;
		// 文件数
		$_Return ['total_count'] = count ( $_FileList );
		// 文件列表数组
		$_Return ['file_list'] = $_FileList;
		// 输出JSON字符串
		header ( 'Content-type: application/json; charset=UTF-8' );
		exit ( json_encode ( $_Return ) );
	}
}